<template>
  <div class="search-form">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="运单号" prop="shipmentCode">
        <el-input
          v-model="queryParams.shipmentCode"
          placeholder="请输入运单号"
          clearable
          style="width: 200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择运单状态"
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="option in statusOptions"
            :key="option.value"
            :label="option.label"
            :value="option.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="目的省份" prop="destinationProvince">
        <el-input
          v-model="queryParams.destinationProvince"
          placeholder="请输入目的省份"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="目的城市" prop="destinationCity">
        <el-input
          v-model="queryParams.destinationCity"
          placeholder="请输入目的城市"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { shipmentStatusOptions } from '../api/comprehensive'

export default {
  name: 'SearchForm',
  props: {
    // 显示搜索条件
    showSearch: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shipmentCode: undefined,
        status: undefined,
        destinationProvince: undefined,
        destinationCity: undefined,
        startDate: undefined,
        endDate: undefined
      },
      // 日期范围
      dateRange: [],
      // 状态选项
      statusOptions: shipmentStatusOptions
    }
  },
  watch: {
    // 监听日期范围变化
    dateRange(val) {
      if (val && val.length === 2) {
        this.queryParams.startDate = val[0]
        this.queryParams.endDate = val[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.$emit('query', { ...this.queryParams })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 重置表单
    resetForm(refName) {
      if (this.$refs[refName]) {
        this.$refs[refName].resetFields()
      }
    },
    /** 获取查询参数 */
    getQueryParams() {
      return { ...this.queryParams }
    },
    /** 设置查询参数 */
    setQueryParams(params) {
      Object.assign(this.queryParams, params)
      if (params.startDate && params.endDate) {
        this.dateRange = [params.startDate, params.endDate]
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.search-form {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  margin-bottom: 10px;
  
  .el-form--inline .el-form-item {
    margin-right: 20px;
    margin-bottom: 15px;
  }
}
</style>